<?php
class RoleUserModel extends Model{
	protected $table='kq_role_user';
	protected $pk='user_id';
	//表中的字段
	/*protected $fields=array('uid','uname','paswd','logtime',
		'logip','status');
	//自动验证规则
	protected $rules=array(
			array('uname',1,'用户名不能为空','require'),
			array('uname',1,'用户名必须在4-16字符内','length','4,16'),
			array('paswd',1,'密码不能为空','require')
			
			
		);
	//自动填充规则
	protected $leftfields=array(
		    array('status','value','1'),
			array('logtime','function','time')
		);

	
	protected function encPasswd($passwd){
		return md5($passwd);
	}
*/
	public function findRoleId($id){
    	$sql='select * from '.$this->table.' where user_id='.$id;
    	return $this->db->getRow($sql);

    }

    public function updateRole($role_id,$uid){
    	
    	$sql='update '.$this->table.' set role_id='.$role_id.' where user_id='.$uid;
    	$res=$this->db->query($sql);
    	if($res){
    		return $this->db->affected_rows();
    	}else{
    		return false;
    	}
    }

    public function deleteRole($uid){
        $sql='delete from '.$this->table.' where user_id='.$uid.' limit 1';
    	$res=$this->db->query($sql);
    	if($res){
    		return $this->db->affected_rows();
    	}else{
    		return false;
    	}
    }

    /*
    *查询当前用户对应的角色
    **/
    public function getRole(){
    	$sql = "select name from kq_role,kq_role_user where kq_role.role_id=kq_role_user.role_id 
    	and kq_role_user.user_id={$_SESSION['user_id']}";
    	return $this->db->getOne($sql);
    }

    /*
    *查询当前用户对应的角色id
    **/
    public function getRoleId(){
        $sql = "select kq_role_user.role_id from kq_role,kq_role_user where kq_role.role_id=kq_role_user.role_id 
        and kq_role_user.user_id={$_SESSION['user_id']}";
        return $this->db->getOne($sql);
    }
}
?>